angular.module('update-pwd', ['head-toast'])

.controller 'updatePwdCtrl', ($scope, $state, $http, $ionicHistory, $ionicPopup, userData, showToast) ->
    _.extend $scope,
        user:
            oldpass: undefined
            newpass: undefined
            confirm: undefined
        formValidation: ->
            if $scope.user.newpass.match(/^[0-9a-zA-Z]{6,16}$/) == null
                showToast '请输入由6-16位英文字母或数字组成的密码'
                return false
            else if $scope.user.newpass != $scope.user.confirm
                showToast '两次输入的新密码不一样,请重新输入!'
                return false
            else
                return true

        goBack: ->
            $ionicHistory.goBack()
        goSave: ->
            if $scope.formValidation()
                $http.post('/users/', {
                    action: 'editpass',
                    userid: userData.getToken(),
                    oldpass: $scope.user.oldpass,
                    newpass: $scope.user.newpass
                })
                .success (resp) ->
                    if resp?.status == '0'
                        showToast '密码修改成功'
                        $ionicHistory.goBack()
                    else
                        showToast resp.msg
                .error ->
                    showToast '您的网络又调皮了'
        clear: (i) ->
            if i == 1
                $scope.user.oldpass = ''
            else if i == 2
                $scope.user.newpass = ''
            else if i == 3
                $scope.user.confirm = ''
